﻿package jlib.component.panel {
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import jlib.component.panel.event.WindowEvent;
	import jlib.component.IOpenable;

	/**
	 * 윈도우 클래스 setMoveableRect 를 설정해주면 창을 움직일 수 있고, content 객체에 data를 통해 
	 * @author 박종희 
	 */	
	public class Window extends MovieClip implements IOpenable
	{
		private var tempMouse:Point;
		
		private var moveableRect:Rectangle;
		private var container:DisplayObjectContainer;
		private var moveable:Boolean;
		private var activeableRect:Rectangle;
		
		public function Window(): void { tempMouse = new Point(); }
		
		private function mouseHandler(e:Event): void
		{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					tempMouse.x = this.mouseX, tempMouse.y = this.mouseY;
					if(moveableRect.containsPoint(tempMouse)){
						stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler );
						stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler );
					}
				break;
				case MouseEvent.MOUSE_MOVE:
					this.x = stage.mouseX-tempMouse.x;
					this.y = stage.mouseY-tempMouse.y;
					if(activeableRect) checkMove();
				break;
				case MouseEvent.MOUSE_UP:
					stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseHandler );
					stage.removeEventListener(MouseEvent.MOUSE_UP, mouseHandler );
				break;
			}
		}
		
		private function checkMove(): void
		{
			if(this.x < activeableRect.x) this.x = activeableRect.x;
			if(this.y < activeableRect.y) this.y = activeableRect.y;
			if(activeableRect.x+activeableRect.width < this.x+this.width) this.x = (activeableRect.x+activeableRect.width)-this.width;
			if(activeableRect.y+activeableRect.height < this.y+this.height) this.y = (activeableRect.y+activeableRect.height)-this.height;
		}
		
		public function setActiveableRect($rect:Rectangle): void
		{
			activeableRect = $rect;
		}
		
		public function open(e:Event=null): void
		{
			if(!container) throw new Error('Container 값이 없습니다.');
			this.container.addChild(this);
			_open();
			this.dispatchEvent(new Event(WindowEvent.OPEN));
		}
		
		public function close(e:Event=null): void 
		{
			if(!this.parent) return;
			if(this.container && this.container.contains(this)) this.container.removeChild(this);
			else this.parent.removeChild(this);
			_close();
			this.dispatchEvent(new Event(WindowEvent.CLOSE));
		}
		
		protected function _open(): void { /*templete method;*/ }
		protected function _close(): void { /*templete method;*/ }
		
		public function getMoveable(): Boolean { return moveable; }
		public function setMoveable(value:Boolean): void 
		{ 
			if(!moveableRect) throw new Error('mouseHandler: moveableRect 이가 없습니다. setMoveableRect로 설정해주세요.');
			moveable = value;
			if(moveable) this.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler );
			else this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseHandler );
		}
		
		public function getMoveableRect(): Rectangle { return moveableRect; }
		public function setMoveableRect(rect:Rectangle): void { this.moveableRect = rect; }
		
		public function getContainer() : DisplayObjectContainer { return container; }
		public function setContainer(container:DisplayObjectContainer): void { this.container = container; }
		
	}//c
}